package com.example.validatestudy.demos.algorithm.sort;

import java.util.Arrays;

/**
 * @param
 * @version v1
 * @auther WANGZHENFANG004
 * @summary 快速排序
 * @since 2024 06 26 19 03
 */
public class QuickSort {

    public static void main(String[] args){
        int[] arr = {30,40,60,10,20,50};
        arr = quick(arr,0,arr.length-1);
        for(int i=0;i<arr.length;i++){
            System.out.println(arr[i]);
        }
    }

    public static int[] quick(int[] arr,int l,int r){
        if(l<r) {
            int i = l;
            int j = r;
            int base = arr[l];
            while (i < j) {
                while (i < j && base < arr[j])
                    j--;
                if (i<j) {
                    arr[i++] = arr[j];
                }
                while (i < j && base > arr[i])
                    i++;
                if (i<j) {
                    arr[j--] = arr[i];
                }
            }
            arr[i] = base;
            System.out.println(Arrays.toString(arr));
            quick(arr, l, i-1);
            quick(arr, i + 1, r);
        }
        return arr;
    }
}
